package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class CryBookContentActivity extends BaseActivity implements View.OnClickListener{

    private ImageView lifeback;
    private LinearLayout lifesearch;
    private ImageView backmain;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private TextView bookname;
    private TextView bookamount;
    private TextView bookcase;
    private TextView bookhot;
    private LinearLayout layCase;
    private LinearLayout layHot;
    private TextView bao;
    private TextView weight;
    private TextView shou;
    private TextView alldis;
    private ImageView jiajiaimg;
    private ImageView jiajiimg;
    private ImageView xiaofeiimg;
    private TextView bookintro;
    private TextView bookcode;
    private TextView sendad;
    private TextView recipientor;
    private TextView recipientad;
    private RelativeLayout map;
    private TextView senddis;
    private TextView recipientdis;
    private TextView sendtime;
    private TextView picktime;
    private Button crybookbt;
    private LinearLayout weiyixiabt;
    private TextView bookstate;
    private RelativeLayout crybookmapview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fw_life);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        initLifeView();
        String id = getIntent().getStringExtra("id");
        getBookContent(id);
    }
        // 获取订单详情
    private void getBookContent(String id) {

        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.BOOKCONTENT);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.addParameter("params", j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 订单详情--"+result);
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    getData(result);
                }else {}
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 详情--"+ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 订单数据
    private void getData(String result) {
        JSONObject obj = JSON.parseObject(result);
        String data = obj.getString("data");
        JSONObject obj1 = JSON.parseObject(data);
        String type = obj1.getString("type");
        String appointTime = obj1.getString("appointTime");
        String consignorAddr = obj1.getString("consignorAddr");
        String consignorHous = obj1.getString("consignorHous");
        String consignorLat = obj1.getString("consignorLat");
        String consignorLong = obj1.getString("consignorLong");
        String consignorName = obj1.getString("consignorName");
        String consignorTel = obj1.getString("consignorTel");
        String insurance = obj1.getString("insurance");
        String intro = obj1.getString("intro");
        String makeupCount = obj1.getString("makeupCount");
        String orderCode = obj1.getString("orderCode");
        String orderId = obj1.getString("orderId");
        String publishTime = obj1.getString("publishTime");
        String sendStatus = obj1.getString("sendStatus");
        String totalAmount = obj1.getString("totalAmount");
        JSONArray introImage = obj1.getJSONArray("introImage");
        bookamount.setText(totalAmount+" 元");
        bookcode.setText(orderCode);
        bookintro.setText(intro);

        sendad.setText(consignorAddr+" "+consignorHous);
        sendtime.setText(CastTime.startTime(Long.valueOf(publishTime)));
        picktime.setText(CastTime.startTime(Long.valueOf(appointTime)));
        shou.setText(consignorName+" "+consignorTel);
        if (type.equals("1")){
            bookname.setText("一键发单");
        }else if (type.equals("2")){
            bookname.setText("急件要发");
            bao.setText(insurance+" 元");
            crybookmapview.setVisibility(View.VISIBLE);
            if (sendStatus.equals("1")) {
                bookstate.setText("待抢单");
            } else if (sendStatus.equals("2")) {
                bookstate.setText("待选定");
            } else if (sendStatus.equals("3")) {
                bookstate.setText("待取货");
            } else if (sendStatus.equals("4")) {
                bookstate.setText("配送中");
            } else if (sendStatus.equals("5")) {
                bookstate.setText("待完结");
            } else if (sendStatus.equals("6")) {
                bookstate.setText("已完结");
            } else if (sendStatus.equals("0")) {
                bookstate.setText("未支付订单");
            } else if (sendStatus.equals("-1")) {
                bookstate.setText("已投诉");
            } else if (sendStatus.equals("-2")) {
                bookstate.setText("订单已取消");
            }
            String tip = obj1.getString("tip");
            String urgent = obj1.getString("urgent");
            String weightt = obj1.getString("weight");
            if (weightt.equals("0")){
                weight.setText("0-2kg");
            }else if (weightt.equals("1")){
                weight.setText("2-5kg");
            }else if (weightt.equals("2")){
                weight.setText("5-10kg");
            }else if (weightt.equals("3")){
                weight.setText("10-20kg");
            }
            if (urgent.equals("true")){// 加急
                jiajiimg.setVisibility(View.VISIBLE);
            }else {
                jiajiimg.setVisibility(View.GONE);
            }
            if (tip.equals("0")){// 小费

            }else {
                switch (Integer.valueOf(tip)){
                    case 1:
                        bookcase.setText("5");
                        break;
                    case 2:
                        bookcase.setText("10");
                        break;
                    case 3:
                        bookcase.setText("20");
                        break;
                }
                layCase.setVisibility(View.VISIBLE);
                layHot.setVisibility(View.VISIBLE);
            }
            if (makeupCount.equals("0")){ // 加价次数为 0

            }else { // 不为 0
                bookhot.setText(makeupCount);
                layHot.setVisibility(View.VISIBLE);
            }
            JSONArray consignee = obj1.getJSONArray("consignee");
            for (int i = 0; i < consignee.size(); i++) {

                JSONObject obj2 = JSON.parseObject((String) consignee.get(i));
                String consigneeAddr = obj2.getString("consigneeAddr");
                String consigneeHous = obj2.getString("consigneeHous");
                String consigneeLat = obj2.getString("consigneeLat");
                String consigneeLong = obj2.getString("consigneeLong");
                String consigneeName = obj2.getString("consigneeName");
                String consigneeTel = obj2.getString("consigneeTel");
            }
            JSONObject obj3 = JSON.parseObject((String) consignee.get(0));
            recipientor.setText(obj3.getString("consigneeName")+" "+obj3.getString("consigneeTel"));
            recipientad.setText(obj3.getString("consigneeAddr")+" "+obj3.getString("consigneeHous"));
        }else if (type.equals("3")){
            bookname.setText("跑腿劳务");
            if (sendStatus.equals("1")) {
                bookstate.setText("待抢单");
            } else if (sendStatus.equals("2")) {
                bookstate.setText("待选定");
            } else if (sendStatus.equals("3")) {
                bookstate.setText("待服务");
            } else if (sendStatus.equals("4")) {
                bookstate.setText("服务中");
            } else if (sendStatus.equals("5")) {
                bookstate.setText("待完结");
            } else if (sendStatus.equals("6")) {
                bookstate.setText("已完结");
            } else if (sendStatus.equals("0")) {
                bookstate.setText("未支付订单");
            } else if (sendStatus.equals("-1")) {
                bookstate.setText("已投诉");
            } else if (sendStatus.equals("-2")) {
                bookstate.setText("订单已取消");
            }
        }else {}

    }

    private void initLifeView() {
        lifeback = (ImageView) findViewById(R.id.life_back);
        backmain = (ImageView) findViewById(R.id.crybookcontent_backmain);

        bookname = (TextView) findViewById(R.id.crybookcontent_bookname);
        bookamount = (TextView) findViewById(R.id.crybookcontent_bookamount);
        bookcase = (TextView) findViewById(R.id.crybookcontent_bookcase);
        bookhot = (TextView) findViewById(R.id.crybookcontent_bookhot);
        layCase = (LinearLayout) findViewById(R.id.crybookcontent_caset);
        layHot = (LinearLayout) findViewById(R.id.crybookcontent_hott);
        bao = (TextView) findViewById(R.id.crybookcontent_bao);
        weight = (TextView) findViewById(R.id.crybookcontent_weight);
        shou = (TextView) findViewById(R.id.crybookcontent_shou);
        alldis = (TextView) findViewById(R.id.crybookcontent_alldis);
        jiajiaimg = (ImageView) findViewById(R.id.crybookcontent_jiajiaimg);
        jiajiimg = (ImageView) findViewById(R.id.crybookcontent_jiajiimg);
        xiaofeiimg = (ImageView) findViewById(R.id.crybookcontent_xiaofeiimg);
        bookintro = (TextView) findViewById(R.id.crybookcontent_bookintro);
        bookcode = (TextView) findViewById(R.id.crybookcontent_bookcode);
        sendad = (TextView) findViewById(R.id.crybookcontent_sendad);
        recipientor = (TextView) findViewById(R.id.crybookcontent_recipientor);
        recipientad = (TextView) findViewById(R.id.crybookcontent_recipientad);
        map = (RelativeLayout) findViewById(R.id.crybookcontent_map);
        senddis = (TextView) findViewById(R.id.crybookcontent_senddis);
        recipientdis = (TextView) findViewById(R.id.crybookcontent_recipientdis);
        sendtime = (TextView) findViewById(R.id.crybookcontent_sendtime);
        picktime = (TextView) findViewById(R.id.crybookcontent_picktime);
        crybookbt = (Button) findViewById(R.id.crybookcontent_crybookbt);
        weiyixiabt = (LinearLayout) findViewById(R.id.crybookcontent_weiyixiabt);
        bookstate = (TextView) findViewById(R.id.crybookcontent_bookstate);
        crybookmapview = (RelativeLayout) findViewById(R.id.crybookcontent_mapview);
        lifeback.setOnClickListener(this);
        backmain.setOnClickListener(this);
        crybookbt.setOnClickListener(this);
        weiyixiabt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.life_back:
                finish();
                break;
            case R.id.crybookcontent_backmain:
                startActivity(new Intent(CryBookContentActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.crybookcontent_crybookbt:

                break;
            case R.id.crybookcontent_weiyixiabt:
                Toast.makeText(this, "功能开发中，敬请期待", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
